/**
 * 此文件会被其它模块相对引入，不要import表单独有信息，若修改每个引入模块需独立打包
 * @author liuzy 2018-11-28
 */

/**
 * 跳转表单详情，统一由此入口控制跳转，兼容流程中心、门户等等入口
 * @param {String/JSON} params JSON对象或url参数串(不需要传url地址)
 * @param {Function} callbackFun 表单关闭时回调事件
 * @param {String} sourceKey 标示不同的调用来源,比如(wfCenter/portal/agent等等),预留参数
 * @param {String} returnUrl 返回跳转至指定链接(只能非EM生效)，不传默认为打开表单来源的url
 */
export const forwardViewForm = (params="", callbackFun, sourceKey="", returnUrl="") => {
    // 重写url(不跳转)，再location.href就会跳转到表单页面，回退时会先回退到mobileForward.html内做跳转处理判断
    // window.history.replaceState(null, document.title, "/spa/workflow/mobileForward.html");
    // const url = `/spa/workflow/forwardMobileForm.html?${urlParams}`;     //跳中转页sessionid就变了
    window.openLink.openWorkflowByParams(params, callbackFun, returnUrl);
}

//跳转至指定链接(异构系统等打开表单)
export const forwardViewPage = (url, callbackFun, sourceKey="", returnUrl="") => {
    window.openLink.openWorkflow(url, callbackFun, returnUrl);
}

//拼接url链接
export const buildUrlStr = (params = {}) => {
    let str = "";
    Object.keys(params).map(key => {
        str += `&${key}=${params[key]}`;
    });
    if (str !== "")
        str = str.substring(1);
    return str;
}

//url参数串转成对象
export const transQueryParams = (arg) => {
    let paramStr = typeof arg === "object" ? arg.search : arg;
    if(paramStr.indexOf("?") > -1)
        paramStr = paramStr.substring(paramStr.indexOf("?")+1);
    let queryParams = {};
    const arr = paramStr.split("&");
    arr && arr.map(item => {
        const index = item.indexOf("=");
        if(index > -1){
            const key = item.substring(0, index);
            const value = decodeURIComponent(item.substring(index+1));   //要转码
            queryParams[key] = value;
        }
    });
    return queryParams;
}